package com.tianbao54.concurrent.atomic;

import com.tianbao54.concurrent.look.AtomicIntegerWithLook;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicTest {

    private static final AtomicInteger integer = new AtomicInteger(1);
    private static final AtomicIntegerWithLook atomicInteger = new AtomicIntegerWithLook(0);
    private static int i = 0;

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0 ; i < 10 ; i ++) {
            executor.execute(() -> {
                for (int j = 0; j < 100 ; j ++) {
                    integer.incrementAndGet();
                    atomicInteger.incrementAndGet();
                    AtomicTest.i ++;
                }
            });
        }
        // 等待所有的线程完成任务..
        executor.awaitTermination(2, TimeUnit.SECONDS);

        // 结束，不能添加新的任务
        //        executor.shutdown();

        System.out.println("i = " + i);
        System.out.println("integer = " + integer.get());
        System.out.println("atomicInteger = " + atomicInteger.getValue());
    }
}
